package com.sicnu.cheer.leaugebar.activity.profileset;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.sicnu.cheer.generalmodule.config.AppManager;
import com.sicnu.cheer.generalmodule.util.DateUtil;
import com.sicnu.cheer.generalmodule.util.SharedPreferencesUtil;
import com.sicnu.cheer.profileset.R;
import com.sicnu.cheer.profileset.config.Constant;
import com.sicnu.cheer.profileset.datepicker.DatePicker;
import com.sicnu.cheer.profileset.utils.ImageUtil;

import java.util.Calendar;
import java.util.Locale;

import cn.aigestudio.datepicker.cons.DPMode;


/**
 * Created by cheer on 2017/2/10.
 * 个人资料导航页，用户第一次登录时对个人资料的设置之生日设置
 */
public class BirthdaySetActivity extends AppCompatActivity implements View.OnClickListener {

    private BirthdaySetActivity mThis;
    private DatePicker picker;
    private String birthday;
    private ImageView sexIV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_birthday_set);
        mThis = this;
        AppManager.getAppManager().addActivity(mThis);
        initView();
    }

    private void initView() {
        picker = ((DatePicker) findViewById(R.id.birthday_dp));
        setDatePicker();
        //展示已选择的性别
        sexIV = ((ImageView) findViewById(R.id.sex_iv));
        ImageUtil.setSexIV(mThis,sexIV);

        findViewById(R.id.forward_step_tv).setOnClickListener(this);
        findViewById(R.id.next_step_tv).setOnClickListener(this);
    }

    private void setDatePicker() {
        Calendar instance = Calendar.getInstance(Locale.CHINA);
        int year = instance.get(Calendar.YEAR);

        //设置默认日期为当前日期的前20年
        picker.setDate(year-20,1);
        picker.setFestivalDisplay(false);
        picker.setTodayDisplay(true);
        picker.setHolidayDisplay(false);
        picker.setDeferredDisplay(false);
        picker.setMode(DPMode.SINGLE);
        picker.setOnDatePickedListener(new DatePicker.OnDatePickedListener() {
            @Override
            public void onDatePicked(String date) {
                birthday= DateUtil.formateDate(DateUtil.convertDate(date,Constant.yyyy_MM_dd_PATTERN),Constant.yyyy_MM_dd_PATTERN);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.forward_step_tv){
            finish();
        }else if (v.getId()==R.id.next_step_tv){
            //保存生日
            SharedPreferencesUtil.saveUserItem(mThis, Constant.BIRTHDAY, birthday);
            //跳转到家乡设置界面
            Intent intent=new Intent(mThis,HometownSetActivity.class);
            startActivity(intent);
        }
    }
}
